const myDb = require('../../../libs/mydb');

//时间
const time = require('../../../utiles/time')

const clearcache = require('../../../utiles/clearcache')
const fs = require('fs')

module.exports = {

    //文章渲染  回传昵称、发布时间、内容或图片、文章id
    articlelist_show: async function (req, res) {
        await myDb.singleSearch('ac_hot_texts', 'ac_hot_texts.tid,ac_hot_texts.release_time,ac_hot_texts.content_text,ac_picture.puc_id,ac_picture.puc_url,ac_user_info.u_nick,ac_user_info.u_headers,COUNT(ac_like_tatble.uid)AS uidNum'
            , 'LEFT JOIN ac_picture ON ac_hot_texts.tid = ac_picture.tid LEFT JOIN ac_like_tatble ON  ac_hot_texts.tid = ac_like_tatble.hot_text_id INNER JOIN ac_user_info ON ac_user_info.uid = ac_hot_texts.uid GROUP BY ac_hot_texts.tid DESC ORDER BY ac_hot_texts.tid DESC')
            .then(
                result => {
                    if (result) {
                        res.send({
                            code: 200,
                            msg: "success",
                            articlelistData: result
                        })
                    } else {
                        res.send({
                            code: 400,
                            msg: 'fail',
                        })
                    }

                }
            )
    },

    //评论渲染
    show_comments: async function (req, res) {
        await myDb.singleSearch('ac_comment',
            'ac_comment.uid,ac_comment.com_id,ac_comment.com_text,ac_comment.com_time,ac_user_info.u_nick,ac_user_info.u_headers,ac_comment.text_id',
            ` LEFT JOIN ac_user_info ON ac_comment.uid = ac_user_info.uid WHERE ac_comment.text_id =${req.query.tid}  ORDER BY ac_comment.com_id DESC`)
            .then(
                result => {
                    res.send({
                        code: 200,
                        msg: "success",
                        commentsData: result
                    })
                }
            )

    },


    //详细内容渲染 传文章id   回传发布时间、文章内容、昵称、评论内容、评论时间,点赞ID的条数（点赞数）
    article_show: async function (req, res) {
        let { tid } = req.body;
        await myDb.singleSearch('ac_hot_texts', 'ac_hot_texts.tid,ac_hot_texts.release_time,ac_hot_texts.content_text,ac_picture.puc_id,ac_picture.puc_url,ac_user_info.u_nick,ac_user_info.u_headers'
            , `LEFT JOIN ac_picture ON ac_hot_texts.tid = ac_picture.tid INNER JOIN ac_user_info ON ac_user_info.uid = ac_hot_texts.uid WHERE ac_hot_texts.tid=${tid} DESC`)
            .then(
                result => {
                    if (result) {
                        res.send({
                            code: 200,
                            msg: 'succes',
                            articleData: result
                        })
                    } else {
                        res.send({
                            code: 400,
                            msg: 'No article',
                        })
                    }
                }
            )
    },

    //发布文章 传用户ID、内容和图片url、发布时间
    article_add: async function (req, res) {
        let data = req.body;
        console.log(data)
        if (data.puc_url != '') {

            data.puc_url.forEach(item => {
                console.log(item);
                clearcache.movefolder(item)
            });
            clearcache.clearfolder()
            let dataA = {
                "uid": data.uid,
                "release_time": data.release_time,
                "content_text": data.content_text
            };
            myDb.add('ac_hot_texts', dataA, '')
                .then(
                    result => {
                        let dataB = {
                            "tid": result.insertId,
                            "puc_url": data.puc_url
                        }
                        myDb.add('ac_picture', dataB, '')
                            .then(
                                result => {
                                    res.send({
                                        code: 200,
                                        msg: 'success'
                                    })
                                }
                            )

                    }
                )
        } else {
            let dataC = {
                "uid": data.uid,
                "release_time": data.release_time,
                "content_text": data.content_text
            };
            await myDb.add('ac_hot_texts', dataC, '')
                .then(
                    result => {
                        res.send({
                            code: 200,
                            msg: 'success',
                        })
                    }
                )
        }

    },
    //点赞功能
    thumbs_up: async function (req, res) {
        let data = req.body;
        let newTime = time(Date.now());
        let textFlage = true;
        let flage = true;
        //查询是否在点赞表中有此文章ID
        await myDb.singleSearch('ac_like_tatble', 'hot_text_id', 'WHERE uid IS NOT NULL')
            .then(
                result => {
                    for (let x in result) {
                        if (data.tid != result[x].hot_text_id) {
                            textFlage = true;
                        } else if (data.tid == result[x].hot_text_id) {
                            textFlage = false;
                            break;
                        }
                    }

                }
            )
        if (textFlage == true) {
            let dataT = {
                "hot_text_id": data.tid,
                "uid": data.uid,
                "host_time": newTime
            }
            myDb.add('ac_like_tatble', dataT, '')
                .then(
                    result => {
                        res.send({
                            code: 200,
                            msg: '点赞成功',
                            thumbsupData: '1'
                        })
                    }
                )
        }
        if (textFlage == false) {
            await myDb.singleSearch('ac_like_tatble', 'uid', `WHERE hot_text_id = ${data.tid}`)
                .then(
                    result => {
                        for (let x in result) {
                            if (data.uid != result[x].uid) {
                                flage = false;

                            } else if (data.uid == result[x].uid) {
                                flage = true;
                                break;
                            }
                        }
                        if (flage == false) {
                            let dataA = {
                                "hot_text_id": data.tid,
                                "uid": data.uid,
                                "host_time": newTime
                            }
                            myDb.add('ac_like_tatble', dataA, '')
                                .then(
                                    result => {
                                        myDb.singleSearch('ac_like_tatble', 'COUNT(fab_id)', `WHERE hot_text_id=${data.tid}`)
                                            .then(
                                                result => {
                                                    res.send({
                                                        code: 200,
                                                        msg: '点赞成功',
                                                        thumbsupDataCG: result
                                                    })
                                                }
                                            )
                                    }
                                )
                        } else if (flage == true) {
                            myDb.del('ac_like_tatble', `WHERE hot_text_id = ${data.tid} AND uid = ${data.uid}`)
                                .then(
                                    result => {
                                        myDb.singleSearch('ac_like_tatble', 'COUNT(fab_id)', `WHERE hot_text_id=${data.tid}`)
                                            .then(
                                                result => {
                                                    console.log(result)
                                                    res.send({
                                                        code: 201,
                                                        msg: '取消点赞成功',
                                                        thumbsupDataQx: result
                                                    })
                                                }
                                            )
                                    }
                                )
                        }

                    }
                )
        }

    },

    //评论功能
    release_comments: async function (req, res) {
        let data = req.body;
        data.com_time = time(Date.now());
        await myDb.add('ac_comment', data, '')
            .then(
                result => {
                    console.log(result)
                    res.send({
                        code: 200,
                        msg: 'Added successfully'
                    })
                }
            )
    }


}